﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Core.Mapping;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Infrastructure;
using System.Windows;
using LinnerToolkit.Desktop.ModernUI.Windows;
using SimulationPTSystem.DAL;
using SimulationPTSystem.Views;

namespace SimulationPTSystem
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : ModernApplication
    {
        public App()
        {
            RunApp("MainWindow/LoginPage");
        }

        protected override async void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            Database.SetInitializer<SimulationPTContext>(null);

            var dbContext = SimulationPTContext.Instance;
            var objectContext = ((IObjectContextAdapter)dbContext).ObjectContext;
            var mappingCollection = (StorageMappingItemCollection)objectContext.MetadataWorkspace.GetItemCollection(DataSpace.CSSpace);
            mappingCollection.GenerateViews(new List<EdmSchemaError>());

            SimulationPTDAL.Initialize();

            if (await dbContext.ChengYuanInfo.CountAsync() == 0)
            {
                List<ChengYuanInfo> chengYuanInfos = new List<ChengYuanInfo>();
                for (int i = 0; i < 20000; i++)
                {
                    chengYuanInfos.Add(new ChengYuanInfo
                    {
                        Code = (i + 1).ToString(),
                        Name = (i + 1).ToString(),
                        Age = i % 100 + 1,
                        Gender = i % 2 == 0 ? "男" : "女",
                        AnswerDeviceID = i % 50 + 1,
                        SensorDeviceID = i % 50 + 1,
                    });
                }
                dbContext.ChengYuanInfo.AddRange(chengYuanInfos);
                await dbContext.SaveChangesAsync();

                List<TeamInfo> teamInfos = new List<TeamInfo>();
                for (int j = 0; j < 400; j++)
                {
                    var teamInfo = new TeamInfo
                    {
                        Code = (j + 1).ToString(),
                        Name = (j + 1).ToString(),
                    };
                    List<ChengYuanInfo> listChengYuanInfo = new List<ChengYuanInfo>();
                    for (int i = j * 50; i < j * 50 + 50; i++)
                    {
                        listChengYuanInfo.Add(chengYuanInfos[i]);
                    }
                    teamInfo.ChengYuanInfos = listChengYuanInfo;
                    teamInfo.ChengYuanCount = listChengYuanInfo.Count;
                    teamInfos.Add(teamInfo);
                }
                dbContext.TeamInfo.AddRange(teamInfos);
                await dbContext.SaveChangesAsync();

                SimulationPTDAL.AddCurrentTrainingTeamInfo(teamInfos[0]);
                var currentTrainingTeamInfo = SimulationPTDAL.GetCurrentTrainingTeamInfo();
                TeamTrainingRec teamTrainingRec = new TeamTrainingRec()
                {
                    TeamInfoID = currentTrainingTeamInfo.TeamInfo.Id,
                    StartTime = DateTime.Now,
                    TeamTrainingRecUUID = currentTrainingTeamInfo.TeamTrainingRecUUID,
                    ComplexTrainingDuration = TimeSpan.FromMinutes(5),
                    ChengYuanTrainingRecs = new List<ChengYuanTrainingRec>(),
                };
                foreach(var chengYuanInfo in currentTrainingTeamInfo.TeamInfo.ChengYuanInfos)
                {
                    teamTrainingRec.ChengYuanTrainingRecs.Add(new ChengYuanTrainingRec()
                    {
                        ChengYuanID = chengYuanInfo.Id,
                        TotalR = 1,
                        TotalC = 2,
                        TotalRC = 3,
                        TrainingDate = teamTrainingRec.StartTime,
                        ComplexTrainingDuration = teamTrainingRec.ComplexTrainingDuration
                    });
                }
                SimulationPTDAL.AddTeamTrainingRec(teamTrainingRec);
            }
        }

        protected override void RegisterTypes()
        {
            RegisterForNavigation<MainWindow>();
            RegisterForNavigation<SplashPage>();
            RegisterForNavigation<LoginPage>();
            RegisterForNavigation<MainPage>();
            RegisterForNavigation<TrainingSettingsPage>();
            RegisterForNavigation<UserInfoPage>();
            RegisterForNavigation<TeamInfoPage>();
            RegisterForNavigation<TeamTrainingRecPage>();
            RegisterForNavigation<ChengYuanTrainingRecPage>();
        }
    }
}
